package org.dromara.hotel.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hotel.domain.bo.TerPriceCalendarBo;
import org.dromara.hotel.domain.vo.TerPayVo;
import org.dromara.hotel.domain.vo.TerPriceCalendarVo;
import org.dromara.hotel.service.ITerPriceCalendarService;
import org.springframework.stereotype.Service;
import org.dromara.hotel.domain.bo.TerHotelRoomsBo;
import org.dromara.hotel.domain.vo.TerHotelRoomsVo;
import org.dromara.hotel.domain.TerHotelRooms;
import org.dromara.hotel.mapper.TerHotelRoomsMapper;
import org.dromara.hotel.service.ITerHotelRoomsService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 酒店房型管理Service业务层处理
 *
 * @author xch
 * @date 2025-06-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TerHotelRoomsServiceImpl implements ITerHotelRoomsService {

    private final TerHotelRoomsMapper baseMapper;
    private final ITerPriceCalendarService  terPriceCalendarService;
    /**
     * 查询酒店房型管理
     *
     * @param id 主键
     * @return 酒店房型管理
     */
    @Override
    public TerHotelRoomsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询酒店房型管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 酒店房型管理分页列表
     */
    @Override
    public TableDataInfo<TerHotelRoomsVo> queryPageList(TerHotelRoomsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerHotelRooms> lqw = buildQueryWrapper(bo);
        Page<TerHotelRoomsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<TerHotelRoomsVo>  queryRoomDetail(Long hotelId ,PageQuery pageQuery){
        return PricePageQuery(hotelId,pageQuery);
    }
    @Override
    public TableDataInfo<TerHotelRoomsVo> queryHome(PageQuery pageQuery){
        return PricePageQuery(null,pageQuery);
    }

    private TableDataInfo<TerHotelRoomsVo> PricePageQuery(Long hotelId,PageQuery pageQuery){
        LambdaQueryWrapper<TerHotelRooms> lqw = Wrappers.lambdaQuery();
        if (hotelId != null) {
            lqw.eq(TerHotelRooms::getHotelId, hotelId);
        }
        lqw.orderByAsc(TerHotelRooms::getOrderNum);
        Page<TerHotelRoomsVo> page=  baseMapper.selectVoPage(pageQuery.build(), lqw);
        TableDataInfo<TerHotelRoomsVo>  list= TableDataInfo.build(page);
        TerPriceCalendarBo bo = new TerPriceCalendarBo();
        bo.setPriceDay(LocalDate.now());
        List<TerPriceCalendarVo> priceList =  terPriceCalendarService.queryList(bo);
        list.getRows().forEach(row -> {
            row.setFinalPrice(row.getPrice());
            priceList.forEach( priceVo -> {
                if(priceVo.getRoomId().equals(row.getId())){
                    row.setFinalPrice(priceVo.getFinalPrice());
                }
            });
        });
        return list;
    }
    /**
     * 查询符合条件的酒店房型管理列表
     *
     * @param bo 查询条件
     * @return 酒店房型管理列表
     */
    @Override
    public List<TerHotelRoomsVo> queryList(TerHotelRoomsBo bo) {
        LambdaQueryWrapper<TerHotelRooms> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerHotelRooms> buildQueryWrapper(TerHotelRoomsBo bo) {
        LambdaQueryWrapper<TerHotelRooms> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerHotelRooms::getOrderNum);
        if(bo!=null){


            lqw.eq(bo.getHotelId() != null, TerHotelRooms::getHotelId, bo.getHotelId());
            lqw.like(StringUtils.isNotBlank(bo.getHotelName()), TerHotelRooms::getHotelName, bo.getHotelName());
            lqw.eq(StringUtils.isNotBlank(bo.getRoomType()), TerHotelRooms::getRoomType, bo.getRoomType());
            lqw.eq(StringUtils.isNotBlank(bo.getRoomImgurl()), TerHotelRooms::getRoomImgurl, bo.getRoomImgurl());
            lqw.eq(StringUtils.isNotBlank(bo.getCarouselImages()), TerHotelRooms::getCarouselImages, bo.getCarouselImages());
            lqw.eq(StringUtils.isNotBlank(bo.getDescription()), TerHotelRooms::getDescription, bo.getDescription());
            lqw.eq(bo.getPrice() != null, TerHotelRooms::getPrice, bo.getPrice());
            lqw.eq(StringUtils.isNotBlank(bo.getLabel()), TerHotelRooms::getLabel, bo.getLabel());
            lqw.eq(bo.getTotalQuantity() != null, TerHotelRooms::getTotalQuantity, bo.getTotalQuantity());
            lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), TerHotelRooms::getSpecification, bo.getSpecification());
            lqw.eq(bo.getOrderNum() != null, TerHotelRooms::getOrderNum, bo.getOrderNum());
        }
        return lqw;
    }

    /**
     * 新增酒店房型管理
     *
     * @param bo 酒店房型管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerHotelRoomsBo bo) {
        TerHotelRooms add = MapstructUtils.convert(bo, TerHotelRooms.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改酒店房型管理
     *
     * @param bo 酒店房型管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerHotelRoomsBo bo) {
        TerHotelRooms update = MapstructUtils.convert(bo, TerHotelRooms.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerHotelRooms entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除酒店房型管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
